﻿using System;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace Poet
{
    public class Picture : MarkupExtension
    {
        public Picture()
        {
            Width = -1;
            Height = -1;
        }

        public Picture(string path)
        {
            Path = path;
        }

        public Picture(string path, int size)
        {
            Path = path;
            Width = Height = size;
        }

        public string Path { get; set; }

        public double Width { get; set; }

        public double Height { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {  
                var uri = new Uri("pack://application:,,/" + Path);
                var image = new BitmapImage(uri);

                if (Height < 0)
                {
                    Height = image.Height;
                }

                if (Width < 0)
                {
                    Height = image.Width;
                }

                return new Image { Source = image, Width = Width, Height = Height };
            }
            catch(Exception)
            {
                return null;
            }
        }
    }
}
